package com.syerit.modules.utils
{
	
	import beherit.fl.preloaders.BasePreloaderUI;
	import beherit.fl.transitions.utils.ITransition;
	import beherit.fl.transitions.utils.ITransitionManager;
	import beherit.fl.transitions.utils.TransitionEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.IBitmapDrawable;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import mx.containers.Canvas;

	public class TransitionManager extends Canvas implements ITransitionManager
	{
		public var transition:ITransition;
		public var preloader:Sprite;
		public var progressBar:*;
		public var background:DisplayObject;
		public var preloaderUI:BasePreloaderUI;
		
		private var _tranCon:Sprite=new Sprite();
		private var _outgoingDisplayObject:IBitmapDrawable;
		private var _outBmd:BitmapData;
		private var _outBm:Bitmap;
		private var _ongoingDisplayObject:IBitmapDrawable;
		private var _onBmd:BitmapData;
		private var _onBm:Bitmap;
		
		public function TransitionManager(_w:Number=1260,_h:Number=600,_preloaderBackground:DisplayObject=null,_info:*=null)
		{
			width=_w;
			height=_h;
			progressBar=_info;
			background=_preloaderBackground;
			this.visible=false;
			super();
		}
		public function set outgoingDisplayObject(obj:IBitmapDrawable):void
		{
			this.visible=true;
			_outBmd=new BitmapData(width,height,true,0);
			_outBmd.draw(obj);
			_outBm=new Bitmap(_outBmd);
			_tranCon.addChild(_outBm);
		}
		public function showLoader():void
		{
			preloaderUI.show();
		}
		public function hideLoader():void
		{
			preloaderUI.hide();
		}
		public function hideLoaderAndChange():void
		{
			preloaderUI.addEventListener(BasePreloaderUI.DISAPPEAR_FINISHED,onHideLoaderFinished);
			preloaderUI.hide();
		}
		public function onHideLoaderFinished(e:Event):void
		{
			change();
		}
		public function set ongoingDisplayObject(obj:IBitmapDrawable):void
		{
			_onBmd=new BitmapData(width,height,true,0);
			_onBmd.draw(obj);
			_onBm=new Bitmap(_onBmd);
		}
		public function set progress(p:Number):void
		{
			preloaderUI.progress=p;
		}
		public function change():void
		{
			trace("change");
			transition.addEventListener(TransitionEvent.FINISHED,onTransitionComplete);
			transition.change(_outBm,_onBm);
		}
		private function onTransitionComplete(e:TransitionEvent):void
		{
			transition.removeEventListener(TransitionEvent.FINISHED,onTransitionComplete);
			dispatchEvent(new TransitionEvent(TransitionEvent.FINISHED));
			this.visible=false;
		} 

		override protected function initializationComplete():void
		{
			//this.alpha = .5;
			super.initializationComplete();
		}
		override protected function commitProperties():void
		{
			this.rawChildren.addChild(_tranCon);
			transition.parentContainer= _tranCon;
			preloaderUI = new BasePreloaderUI();
			preloaderUI.background = background;
			preloaderUI.progressBar = progressBar;
			preloaderUI.alpha=.1;
			//preloaderUI.relativeProgressBarPositionX = 400;
			preloaderUI.createChildren();
			this.rawChildren.addChild(preloaderUI);
			
			super.commitProperties();
		}
	}
}